package com.sls.web.manager.action.recharge;

import javax.annotation.Resource;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.Operation;
import com.sls.web.manager.recharge.querybean.OperationQuery;
import com.sls.web.manager.recharge.service.OperationService;

public class OperationAction extends SuperAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8042339901963961837L;
	@Resource(name = "operationService")
	private OperationService operationService;
	private Pagination<Operation> page;
	private OperationQuery oq;
	private Operation operation;

	public String list() {
		try {
			if (oq == null) {
				oq = new OperationQuery();
				oq.setCurrentPage(Constants.INIT_CURRENTPAGE);
				oq.setPageSize(Constants.INIT_PAGESIZE);
			}
			page = operationService.findByPage(oq, getCurrentOperator());
		} catch (Exception e) {
			addActionError(e.getMessage());
		}
		return "list";
	}

	public String add() {
		if (operation == null) {
			return "edit";
		}
		try {
			operationService.save(operation, getCurrentOperator());
			redirectionUrl = "operation!list.htm";
			return "success";
		} catch (Exception e) {
			addActionError(e.getMessage());
			return "edit";
		}
	}

	public String delete() {
		try {
			operationService.delete(operation, getCurrentOperator());
		} catch (Exception e) {
			addActionError(e.getMessage());
		}
		return list();
	}

	public String update() {
		try {
			if (operation.getCode() != null && operation.getName() == null) {
				operation = operationService.findByCode(operation.getCode(),
						getCurrentOperator());
				return "edit";
			} else {
				operationService.update(operation, getCurrentOperator());
			}
		} catch (Exception e) {
			addActionError(e.getMessage());
		}
		return list();
	}

	public Pagination<Operation> getPage() {
		return page;
	}

	public void setPage(Pagination<Operation> page) {
		this.page = page;
	}

	public OperationQuery getOq() {
		return oq;
	}

	public void setOq(OperationQuery oq) {
		this.oq = oq;
	}

	public void setOperation(Operation operation) {
		this.operation = operation;
	}

	public Operation getOperation() {
		return operation;
	}

}
